package org.example.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.example.common.R;
import org.example.pojo.entity.SysDept;
import org.example.pojo.req.DeptReq;
import org.example.pojo.vo.DeptVo;
import org.example.service.SysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "后台部门接口")
@RestController
@RequestMapping("api/dept")
public class SysDeptController {

    @Autowired
    private SysDeptService sysDeptService;


    /**
     * 获取部门tree
     * @param deptName
     * @param status
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "deptName", value = ""),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "status", value = "")
    })
    @ApiOperation(value = "获取部门tree", notes = "获取部门tree", httpMethod = "GET")
    @GetMapping("/tree")
    public R deptTree(@RequestParam(required = false) String deptName, @RequestParam(required = false) String status) {
        List<DeptVo> deptVoList = sysDeptService.tree(deptName, status);
        return R.ok(deptVoList);
    }

    /**
     * 添加部门信息
     *
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "SysDept", name = "sysDept", value = "", required = true)
    })
    @ApiOperation(value = "添加部门信息", notes = "添加部门信息", httpMethod = "POST")
    @PostMapping("/add")
    public R addOrDept(@RequestBody DeptReq deptReq) {
        sysDeptService.addOrDept(deptReq);
        return R.ok("操作成功");
    }

    /**
     * 修改部门信息
     *
     * @param sysDept
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "SysDept", name = "sysDept", value = "", required = true)
    })
    @ApiOperation(value = "修改部门信息", notes = "修改部门信息", httpMethod = "PUT")
    @PutMapping("/edit")
    public R editDept(@RequestBody SysDept sysDept) {
        sysDeptService.editDept(sysDept);
        return R.ok("修改成功");
    }

    /**
     * 删除部门信息
     *
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "string", name = "id", value = "", required = true)
    })
    @ApiOperation(value = "删除部门信息", notes = "删除部门信息", httpMethod = "DELETE")
    @DeleteMapping("/remove/{id}")
    public R remove(@PathVariable String id) {
        return sysDeptService.removeDept(id);
    }

}
